import {page, pageFunc} from "../common/PageBuilder";
import {BasePageData} from "../common/core/BasePage";

import {Message} from "../../modules/messageModule/data/Message";
import {DataLoader, field} from "../../modules/coreModule/data/DataLoader";
import {ItemDetailPage} from "../common/pages/ItemDetailPage";
import {MessageInterface} from "../../modules/messageModule/MessageCommon";
import {waitForLogin} from "../../modules/userModule/managers/UserManager";
import {UserPage} from "../common/partPages/UserPage";


// @ts-ignore
class Data extends  BasePageData{
	@field(Message)
	item: Message
}

class Param {
	id: string
}

@page("messageDetail", "消息详情")
export class MessageDetailPage extends ItemDetailPage<Data, Message, Param> {

	public data = new Data();

	public userPage: UserPage = new UserPage();

	onLoad(e) {
		super.onLoad(e);
		this.loadData().then()
	}

	@waitForLogin
	private async loadData() {
		const res = await MessageInterface.GetMessage({id: this.params.id})
		const message = DataLoader.load(Message, res.message);
		await this.setData({ item: message })

		await MessageInterface.ReadMessage({id: message._id});
	}

	@pageFunc
	public async deleteMessage() {
		await MessageInterface.DeleteMessage({id: this.data.item._id})
		await this.back();
	}
}
